﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using XmlStudio.Resources.UserControls;
using XmlStudio.Utilities;
using XmlStudio.ViewModel.Node;
using System.Collections;
using System.Collections.Generic;
using XmlStudio.Interfaces;
using System.Linq;
using XmlStudio.DataLayer;
using Tomers.WPF.Localization;

namespace XmlStudio.Dialogs {
    /// <summary>
    /// Interaction logic for EditXsdElementDialog.xaml
    /// </summary>
    public partial class EditXsdElementDialog : Window {
        #region Fields
        private XsdAttributeCollectionViewModel attributes = new XsdAttributeCollectionViewModel();
        private XsdAttributeCollectionViewModel requiredAttributes = new XsdAttributeCollectionViewModel();
        private IXsdModel model;
        #endregion

        #region Properties
        public XmlElementViewModel XmlElement {
            get;
            private set;
        }

        public XsdElement ElementType { get; set; }

        public XsdAttributeCollectionViewModel Attributes {
            get { return this.attributes; }
            set { this.attributes = value; }
        }
        #endregion

        public EditXsdElementDialog() {
            InitializeComponent();
        }

        public EditXsdElementDialog(XmlElementViewModel element, XsdElement elementType, IEnumerable<IBigXmlAttribute> requiredAttributes, IXsdModel model)
            : this() {
            this.model = model;
            this.ucAttributes.Model = model;
            this.XmlElement = element;
            this.ElementType = elementType;
            this.ucAttributes.ElementType = this.ElementType;
            this.DataContext = new { Element = this.XmlElement, Attributes = this.Attributes };

            // add simpleTypes
            this.attributes.Clear();
            foreach(var attribute in element.Attributes) {
                this.attributes.Add(new XsdAttributeViewModel {
                    LocalName = attribute.LocalName,
                    NamespaceURI = attribute.NamespaceURI,
                    Prefix = attribute.Prefix,
                    Value = attribute.Value,
                    Required = false // to do: find out if this attribute is required
                });
            }

            if(requiredAttributes != null) {
                // add required simpleTypes
                foreach(var attribute in requiredAttributes) {
                    this.attributes.Add(new XsdAttributeViewModel {
                        LocalName = attribute.LocalName,
                        NamespaceURI = attribute.NamespaceURI,
                        Prefix = attribute.Prefix,
                        Value = attribute.Value,
                        Required = true
                    });
                }
            }
        }

        protected override void OnInitialized(System.EventArgs e) {
            base.OnInitialized(e);

            this.prefixTextBox.Focus();
        }

        private void OnSubmit(object sender, RoutedEventArgs e) {
            var empty = from x in this.Attributes where ((XsdAttributeViewModel)x).Required && string.IsNullOrEmpty(x.Value) select x;
            if(empty.Count() > 0) {
                MessageBox.Show(LanguageDictionary.GetDictionary(LanguageContext.Instance.Culture).Translate<string>("EditXsdElementDialog.Required", "Text"),
                    LanguageDictionary.GetDictionary(LanguageContext.Instance.Culture).Translate<string>("EditXsdElementDialog.Warning", "Text"),
                    MessageBoxButton.OK,
                    MessageBoxImage.Warning);
                return;
            }

            this.XmlElement.Attributes.Clear();

            foreach(var att in this.Attributes) {
                var a = att as XsdAttributeViewModel;
                //if(a != null) {
                //    //if(a.Required && string.IsNullOrEmpty(a.Value)) {
                //    //    MessageBox.Show("The value is required!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); // TO DO: translate
                //    //}
                //}

                if(!string.IsNullOrEmpty(att.Value)) {
                    this.XmlElement.Attributes.Add(att);
                }
            }

            BindingExpression prefixExpression = prefixTextBox.GetBindingExpression(TextBox.TextProperty);
            BindingExpression localNameExpression = localNameTextBox.GetBindingExpression(TextBox.TextProperty);
            BindingExpression namespaceURIExpression = namespaceURITextBox.GetBindingExpression(TextBox.TextProperty);
            BindingExpression attributesExpression = ucAttributes.GetBindingExpression(UcXsdAttributes.AttributesProperty);

            if(prefixExpression != null)
                prefixExpression.UpdateSource();
            if(localNameExpression != null)
                localNameExpression.UpdateSource();
            if(namespaceURIExpression != null)
                namespaceURIExpression.UpdateSource();
            if(attributesExpression != null)
                attributesExpression.UpdateSource();

            this.DialogResult = true;
            this.Close();
        }
    }
}
